package com.coolgor.xcode;

import java.io.*;
import java.util.List;

public class StackOperationStateEnum {

    public static void run(String basePath, List<String[]> context) throws IOException {
        String outputPath = basePath + XcoderConfig.OutputPath + "/enums";
        String tableName = null;
        String className = null;
        String keyColumn = null;
        String keyParam = null;
        for (int i = 0; i < context.size(); i++) {
            switch (context.get(i)[0].toLowerCase()) {
                case "table":
                    tableName = context.get(i)[1];
                    className = StringUtil.firstUpper(context.get(i)[2]);
                    break;
                case "key":
                    keyColumn = context.get(i)[1];
                    keyParam = context.get(i)[2];
                    break;
            }
        }

        // File writing ###############
        File file = new File(outputPath);
        if (!file.exists()) file.mkdirs();
        file = new File(outputPath + "/" + className + "OperationStateEnum.java");
        if (!file.exists()) file.createNewFile();
        FileOutputStream fos = new FileOutputStream(file);
        BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(fos));
        bw.newLine();
        bw.write("public enum "+className+"OperationStateEnum {\n");
        bw.write("\tSUCCESS(1000, \"操作成功\"),\n");
        bw.write("\tINNER_ERROR(-1001, \"内部系统错误\"),\n");
        bw.write("\tNULL(-1002, \"信息为空\"),\n");
        bw.write("\tNULL_ID(-1003, \"ID为空\"),\n");
        bw.write("\tEMPTY_LIST(-1004, \"数据列表为空\");\n");
        bw.newLine();
        bw.write("\tprivate int state;\n");
        bw.newLine();
        bw.write("\tprivate String stateInfo;\n");
        bw.newLine();
        bw.write("\tprivate "+className+"OperationStateEnum(int state, String stateInfo) {\n");
        bw.write("\t\tthis.state = state;\n");
        bw.write("\t\tthis.stateInfo = stateInfo;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic int getState() {\n");
        bw.write("\t\treturn state;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic String getStateInfo() {\n");
        bw.write("\t\treturn stateInfo;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("\tpublic static "+className+"OperationStateEnum stateOf(int index) {\n");
        bw.write("\t\tfor ("+className+"OperationStateEnum state : values()) {\n");
        bw.write("\t\t\tif (state.getState() == index) {\n");
        bw.write("\t\t\t\treturn state;\n");
        bw.write("\t\t\t}\n");
        bw.write("\t\t}\n");
        bw.write("\t\treturn null;\n");
        bw.write("\t}\n");
        bw.newLine();
        bw.write("}\n");
        bw.close();
    }
}
